// 时机: 在 Jest 环境设置之前运行，即在所有测试运行之前。
// 用途: 适用于在测试环境之前执行一些全局的设置，例如配置全局变量，引入测试环境所需的 polyfills 等


// const jsdom = require('jsdom');
// const { JSDOM } = jsdom;

// const dom = new JSDOM('<!doctype html><html><body></body></html>');
// global.window = dom.window;
// global.document = dom.window.document;


global.window = global.window || {};

Object.defineProperty(global.window, 'addEventListener', {
  value: jest.fn(),
  writable: true,
});

Object.defineProperty(global.window, 'removeEventListener', {
  value: jest.fn(),
  writable: true,
});

global.fetch = jest.fn((param) => {
  if (param.indexOf("/fas/fas-icons.css") != "-1") {
    return Promise.resolve({
      text: () =>
        Promise.resolve(`.icon-t-home {}
            .other-icon {}
            .default-icon{}`),
    });
  }
  Promise.resolve({
    json: () => Promise.resolve({ data: "mocked data" }),
    text: () => Promise.resolve("mocked text"),
  });
});
global.EventSource = jest.fn();
global.EventSource.mockImplementationOnce(() => ({
  addEventListener: jest.fn(),
  close: jest.fn(),
}));

global.eventService = {
  eventBus: {
    _emit: jest.fn(),
    emit: jest.fn(),
  },
};

global.funcMenuService = {
  getAllFuncInfo: jest.fn(() => {
    return Promise.resolve([
      {
        Name: "目标绩效(用户列表)(用户数据权限)",
        NameInitials: "MBJX(YHLB)(YHSJQX)",
        Code: null,
        FunctionType: 0,
        NavigateUrl: "/UserAuth/UserAuthList",
        Description: null,
        Icon: null,
        IconColor: null,
        RouterInfo: null,
        ApplicationSystemID: null,
        FunctionGroupID: null,
        Type: "7",
        ApplicationURL: "https://newsys.teld.cn",
        AppID: null,
        MID: "normal",
        MaxWin: 1,
        ID: "VirtFunc",
        ApplicationID: "51C02297-5233-4682-84B7-VirtAPP00001",
        AppFullName: "目标",
        GroupName: "看板",
      },
    ]);
  }),
};


const originalLocation = window.location;
delete global.window.location;
global.window.location = {
  ...originalLocation,
};

// 解决读cokile没有document.domain 问题
global.document.domain = "localhost.teld.cc";
if (!global.location) global.location = {};
global.location.hostname = "localhost.teld.cc";

/* 指纹使用全局配置 */
const mockNavigator = {
  userAgent: "Mock User Agent",
  language: "en-US",
  cpuClass: "mockCPU",
  hardwareConcurrency: 4,
  doNotTrack: "1",
};

const mockScreen = {
  height: 1080,
  width: 1920,
  pixelDepth: 32,
};

global.window.navigator = mockNavigator;
global.window.screen = mockScreen;

class MockDate extends Date {
  constructor(...args) {
    if (args.length === 0) {
      super("2023-01-01T12:00:00Z");
    } else {
      super(...args);
    }
  }
}

global.Date = MockDate;

MockDate.prototype.format = function(fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return fmt;
};

/* 引入i18n */
import Vue from "vue";
import VueI18n from "../plugins/vue-i18n.common";

Vue.use(VueI18n);

const locale = "zh-CN";

const i18nData = require(`teld-m-plugin/server/web2i18n/${locale}/web.js`);
let messages = {};
messages[locale] = i18nData;
const i18n = new VueI18n({
  locale,
  messages,
});

const $nuxt = {
  $i18n: i18n,
};

window.$nuxt = $nuxt;


